package top.psjj.mall.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.psjj.mall.comment.Result;
import top.psjj.mall.filter.UserFilter;
import top.psjj.mall.model.dto.CreateOrderReq;
import top.psjj.mall.model.vo.OrderVo;
import top.psjj.mall.service.api.OrderService;

import javax.validation.Valid;

/*
* 订单模块*/
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("/order/create") //订单创建
    public Result createOrder(@RequestBody @Valid CreateOrderReq createOrderReq){
        //订单编号
        String orderNo = orderService.createOrder(UserFilter.currentUser.getId(),createOrderReq);
        return Result.success(orderNo);
    }
    @GetMapping("/order/detail") //订单详情
    public Result detail(@RequestParam("orderNo") String orderNo){
        OrderVo orderVo = orderService.detail(orderNo);
        return Result.success(orderVo);
    }
    @GetMapping("/order/list") //订单列表
    public Result list(@RequestParam Integer pageNum,
                       @RequestParam Integer pageSize){
        PageInfo pageInfo = orderService.listForCustomer(pageNum,pageSize);
        return Result.success(pageInfo);
    }
    @PostMapping("order/cancel")
    public Result cancel(@RequestParam("oederNo") String orderNo){
        orderService.cancel(orderNo);
        return Result.success();
    }
    @GetMapping("/order/qrcode")
    public Result qrcode(@RequestParam("orderNo") String orderNo){
        String qrurl = orderService.qrcode(orderNo);
        return Result.success(qrurl);
    }
    @GetMapping("/order/pay")
    public Result pay(@RequestParam("orderNo") String orderNo){
        orderService.pay(orderNo);
        return Result.success();
    }
    @GetMapping("/admin/order/list")
    public Result adminList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        PageInfo pageInfo = orderService.listForAdmin(pageNum,pageSize);
        return Result.success(pageInfo);
    }
    @PostMapping("/admin/order/delivered")
    public Result deliver(@RequestParam("orderNo") String orderNo){
        orderService.deliver(orderNo);
        return Result.success();
    }
    @PostMapping("/order/finish")
    public Result finish(@RequestParam("orderNo") String orderNo){
        orderService.finish(orderNo);
        return Result.success();
    }


}
